<template>
  <view class="form-item">
    <text class="form-label">内容</text>
    <view v-for="(text, index) in contents" :key="index" class="text-item">
      <view class="textarea-wrapper">
        <textarea v-model="contents[index]" class="content-textarea" placeholder="请输入内容" :maxlength="30000"
          :adjust-position="false" @input="handleInput" />
        <button class="delete-button text-delete" @click="handleDelete(index)">
          <image src="/static/icons/close.svg" class="delete-icon" />
        </button>
      </view>
    </view>
    <button class="add-button" @click="handleAdd">添加内容</button>
  </view>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  modelValue: {
    type: Array,
    default: () => ['']
  }
})

const emit = defineEmits(['update:modelValue'])

const contents = ref(props.modelValue)

watch(() => props.modelValue, (newVal) => {
  contents.value = newVal
})

const handleInput = () => {
  emit('update:modelValue', contents.value)
}

const handleAdd = () => {
  contents.value.push('')
  emit('update:modelValue', contents.value)
}

const handleDelete = (index) => {
  contents.value.splice(index, 1)
  emit('update:modelValue', contents.value)
}
</script>

<style lang="scss" scoped>
.form-label{
  display: block;
  margin-bottom: 12rpx;
}
.text-item {
  position: relative;
  margin-bottom: 24rpx;

  .textarea-wrapper {
    position: relative;
  }

  .content-textarea {
    box-sizing: border-box;
    width: 100%;
    min-height: 200rpx;
    padding: 24rpx;
    border: 2rpx solid #dcdfe6;
    border-radius: 12rpx;
    font-size: 28rpx;
    line-height: 1.6;
    background: #fff;
  }

  .delete-button {
    position: absolute;
    right: 16rpx;
    top: 16rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 48rpx;
    height: 48rpx;
    padding: 0;
    background: rgba(0, 0, 0, 0.05);
    border: none;
    border-radius: 50%;
    transition: all 0.3s ease;

    &:active {
      background: rgba(0, 0, 0, 0.1);
    }

    .delete-icon {
      width: 24rpx;
      height: 24rpx;
    }
  }
}
</style>